package com.mnotice.message.aspect;

import com.mnotice.common.entity.message.Notification;
import com.mnotice.message.sender.MessageSender;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Aspect
@Component
public class NotifyListenerAspect {
    @Resource
    private MessageSender sender;

    @Pointcut("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)")
    public void pointCut() {
    }

    @Around(value = "pointCut() && @annotation(annotation)")
    public Object around(ProceedingJoinPoint point, RabbitListener annotation) throws Throwable {
        String queueName = annotation.queues()[0];
        Object[] args = point.getArgs();
        System.out.println("接收到来自：" + queueName + " 的消息");
        return point.proceed();
    }

    @AfterThrowing(pointcut = "pointCut()", throwing = "throwable")
    public void processException(JoinPoint joinPoint, Throwable throwable) {
//        Object[] args = joinPoint.getArgs();
//        sender.send("item.exception", (Notification) args[0]);
    }
}